package org.geektimes.projects.user.repository;

import org.geektimes.projects.user.domain.User;
import org.geektimes.projects.user.sql.DBConnectionManager;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;
import java.util.Collection;
import java.util.List;

/**
 * @author tom
 * @since 2021/3/10 17:23
 */
public class JpaUserRepository implements UserRepository {

    @Resource(name = "bean/DBConnectionManager")
    private DBConnectionManager dbConnectionManager;

    @Override
    public boolean save(User user) {
        EntityManager entityManager = dbConnectionManager.getEntityManager();
        EntityTransaction transaction = entityManager.getTransaction();
        transaction.begin();
        entityManager.persist(user);
        transaction.commit();
        return true;
    }

    @Override
    public boolean deleteById(Long userId) {
        EntityManager entityManager = dbConnectionManager.getEntityManager();
        EntityTransaction transaction = entityManager.getTransaction();
        transaction.begin();
        User user = entityManager.find(User.class, userId);
        entityManager.remove(user);
        transaction.commit();
        return true;
    }

    @Override
    public boolean update(User user) {
        EntityManager entityManager = dbConnectionManager.getEntityManager();
        EntityTransaction transaction = entityManager.getTransaction();
        transaction.begin();
        entityManager.merge(user);
        transaction.commit();
        return true;
    }

    @Override
    public User getById(Long userId) {
        EntityManager entityManager = dbConnectionManager.getEntityManager();
        EntityTransaction transaction = entityManager.getTransaction();
        transaction.begin();
        User user = entityManager.find(User.class, userId);
        transaction.commit();
        return user;
    }

    @Override
    public User getByName(String userName) {
        EntityManager entityManager = dbConnectionManager.getEntityManager();
        Query query = entityManager.createQuery("select u from User u where name = ?1");
        query.setParameter(1, userName);
        List<User> result = query.getResultList();
        if(null == result || result.size() == 0){
            return null;
        }else{
            return result.get(0);
        }
    }

    @Override
    public User getByNameAndPassword(String userName, String password) {
        EntityManager entityManager = dbConnectionManager.getEntityManager();
        Query query = entityManager.createQuery("select u from User u where name = ?1 and password = ?2");
        query.setParameter(1, userName);
        query.setParameter(2, password);
        List<User> result = query.getResultList();
        if(null == result || result.size() == 0){
            return null;
        }else{
            return result.get(0);
        }
    }

    @Override
    public Collection<User> getAll() {
        Query query = dbConnectionManager.getEntityManager().createQuery("select u from User u");
        return query.getResultList();
    }
}
